import { useAuthStore } from "./auth";
import tool from "@/utils/common/index";
import { defineStore } from "pinia";
import { nextTick, ref } from "vue";
import { useRoute } from "vue-router";

export const useSysStore = defineStore("sysStore", () => {
  const isLoading = ref(false); // 加载页面
  const isProcess = ref(false); // 进度条
  const isPageLoading = ref(false); // 主题内容页面加载
  const processFinish = ref(false); // 是否要走完进度条
  const loginLayout = ref(tool.getLocalStore("yu-loginLayout") ?? 0); // 0 居中布局 1 左侧布局 2 右侧布局
  const remberUserPwdLogin = ref<any>(tool.getLocalStore("yu-remberUserPwdLogin")); // 记住的用户名密码
  const isRemberUserPwd = ref<boolean>(tool.getLocalStore("yu-isRemberUserPwd")); // 是否开启记住用户名密码
  const themeMode = ref<any>(tool.getLocalStore("yu-themeMode") ?? "light");
  const isDark = ref<boolean>(tool.getLocalStore("yu-isDark") ?? false);
  const isGrayMode = ref(tool.getLocalStore("yu-isGrayMode") ?? false); // 是否灰色模式
  const isColorWeakMode = ref(tool.getLocalStore("yu-isColorWeakMode") ?? false); // 是否色弱模式
  const isCollapse = ref(tool.getLocalStore("yu-isCollapse") ?? false);
  const layoutMode = ref(tool.getLocalStore("yu-layoutMode") ?? 0); // 0 左侧菜单 1 主顶部栏 2 顶部菜单
  const i18n = ref(tool.getLocalStore("yu-i18n") ?? "en");
  const isShowSettingDrawer = ref(false);
  const isContentFullScreen = ref(false);
  const reloadPageKey = ref(0);
  const isReloadPageing = ref(false);
  const searchPageHistory = ref(tool.getLocalStore("yu-searchPageHistory") ?? []);
  const collectPageList = ref(tool.getLocalStore("yu-collectPageList") ?? []);
  const isShowSearchPageDialog = ref(false);
  const isShowTopLabelBar = ref(tool.getLocalStore("yu-isShowTopLabelBar") ?? false);
  const isShowLogo = ref(tool.getLocalStore("yu-isShowLogo") ?? false);
  const isPersistenceTopLabelBar = ref(tool.getLocalStore("yu-isPersistenceTopLabelBar") ?? false);
  const setIsLoading = (val: boolean) => {
    isLoading.value = val;
  };
  const setIsProcess = (val: boolean) => {
    isProcess.value = val;
  };
  const setProcessFinish = (val: boolean) => {
    processFinish.value = val;
  };
  const setLoginLayout = (val: number) => {
    loginLayout.value = val;
    tool.setLocalStore("yu-loginLayout", val);
  };
  const setRemberUserPwdLogin = (val: any) => {
    if (val) {
      remberUserPwdLogin.value = {
        username: val.username,
        password: val.password,
      };
    } else {
      remberUserPwdLogin.value = val;
    }
    tool.setLocalStore("yu-remberUserPwdLogin", remberUserPwdLogin.value);
  };
  const setIsRemberUserPwd = (val: boolean) => {
    isRemberUserPwd.value = val;
    tool.setLocalStore("yu-isRemberUserPwd", val);
  };
  const setIsGrayMode = (val: boolean) => {
    isGrayMode.value = val;
    tool.setLocalStore("yu-isGrayMode", val);
  };
  const setIsColorWeakMode = (val: boolean) => {
    isColorWeakMode.value = val;
    tool.setLocalStore("yu-isColorWeakMode", val);
  };
  const setIsCollapse = (val: boolean) => {
    isCollapse.value = val;
    tool.setLocalStore("yu-isCollapse", val);
  };
  const setLayoutMode = (val: any) => {
    layoutMode.value = val;
    tool.setLocalStore("yu-layoutMode", val);
  };
  const setIsShowSettingDrawer = (val: any) => {
    isShowSettingDrawer.value = val;
  };
  const setThemeMode = (val: any) => {
    themeMode.value = val;
    tool.setLocalStore("yu-themeMode", val);
  };
  const setIsDark = (val: any) => {
    isDark.value = val;
    tool.setLocalStore("yu-isDark", val);
  };
  const setIsContentFullScreen = (val: any) => {
    isContentFullScreen.value = val;
  };
  const setIsReloadPageing = (val: any) => {
    isReloadPageing.value = val;
  };
  const setCollectPageList = (val: any) => {
    collectPageList.value = val;
    tool.setLocalStore("yu-collectPageList", val);
  };
  // 重新加载页面
  const reloadCurrentView = async (route: any) => {
    setIsReloadPageing(true);
    setIsProcess(true);
    const authStore = useAuthStore();
    console.log(route);
    const name = route.name as string | undefined;

    if (name) {
      // 1) 临时把当前组件从 include 里移除，触发缓存实例销毁
      const list = [...authStore.keepAliveList];
      const idx = list.indexOf(name);
      if (idx !== -1) {
        list.splice(idx, 1);
        authStore.keepAliveList = list; // 或者调用你封装的 set 方法
        await nextTick();
        // 2) 按原位置加回去
        list.splice(idx, 0, name);
        authStore.keepAliveList = list;
      }
    }
    // 3) 变更 key => 强制当前视图重新挂载
    reloadPageKey.value++;
    setProcessFinish(true);
    setTimeout(() => {
      setIsReloadPageing(false);
    }, 300);
  };
  const setI18n = (val: any) => {
    i18n.value = val;
    tool.setLocalStore("yu-i18n", val);
  };
  const setSearchPageHistory = (val: any) => {
    searchPageHistory.value = val;
    tool.setLocalStore("yu-searchPageHistory", val);
  };
  const setIsShowSearchPageDialog = (val: any) => {
    isShowSearchPageDialog.value = val;
  };

  const setIsShowTopLabelBar = (val: any) => {
    isShowTopLabelBar.value = val;
    tool.setLocalStore("yu-isShowTopLabelBar", val);
  };
  const setIsShowLogo = (val: any) => {
    isShowLogo.value = val;
    tool.setLocalStore("yu-isShowLogo", val);
  };
  const setIsPersistenceTopLabelBar = (val: any) => {
    isPersistenceTopLabelBar.value = val;
    tool.setLocalStore("yu-isPersistenceTopLabelBar", val);
  };
  const setIsPageLoading = (val: any) => {
    isPageLoading.value = val;
  };
  return {
    isLoading,
    isProcess,
    processFinish,
    loginLayout,
    isRemberUserPwd,
    remberUserPwdLogin,
    isGrayMode,
    isColorWeakMode,
    isCollapse,
    layoutMode,
    isShowSettingDrawer,
    themeMode,
    isDark,
    isContentFullScreen,
    reloadPageKey,
    isReloadPageing,
    i18n,
    searchPageHistory,
    isShowSearchPageDialog,
    collectPageList,
    isShowTopLabelBar,
    isShowLogo,
    isPersistenceTopLabelBar,
    isPageLoading,
    setIsLoading,
    setIsProcess,
    setProcessFinish,
    setLoginLayout,
    setRemberUserPwdLogin,
    setIsRemberUserPwd,
    setIsGrayMode,
    setIsColorWeakMode,
    setIsCollapse,
    setLayoutMode,
    setIsShowSettingDrawer,
    setThemeMode,
    setIsDark,
    setIsContentFullScreen,
    reloadCurrentView,
    setIsReloadPageing,
    setI18n,
    setSearchPageHistory,
    setIsShowSearchPageDialog,
    setCollectPageList,
    setIsShowTopLabelBar,
    setIsShowLogo,
    setIsPersistenceTopLabelBar,
    setIsPageLoading,
  };
});
